package com.example.tests;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;

import com.example.fw.ApplicationManager;
import static com.example.tests.GroupDataGenerator.generateRandomGroups;
import static com.example.tests.ContactDataGenerator.generateRandomContacts;

public class TestBase {
	
	protected static ApplicationManager app;
	protected static boolean DISABLESORT = com.example.fw.HelperBase.DISABLESORT;

	@BeforeTest
	public void setUp() throws Exception {
		String configFile = System.getProperty("configFile", "application.properties");
		Properties properties = new Properties();
		properties.load(new FileReader(new File(configFile)));
		app = new ApplicationManager(properties);
	  }
	
	@AfterTest
	public void tearDown() throws Exception {
		app.stop();
	  }
	
	@DataProvider
	public Iterator<Object[]> randomValidGroupGenerator() {
		List<Object[]> list = wrapGroupDataForProvider(generateRandomGroups(5));
		return list.iterator();		
	}

	public static List<Object[]> wrapGroupDataForProvider(List<GroupData> groups) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (GroupData group : groups ) {
			list.add(new Object[] {group});
		}
		return list;
	}

	@DataProvider
	public Iterator<Object[]> invalidGroupGenerator() {
		List<Object[]> list = new ArrayList<Object[]>();
		list.add(new Object[] {new GroupData()
		.withName("name '")});			
		return list.iterator();		
	}
		
	@DataProvider
	public Iterator<Object[]> randomValidContactGenerator() throws Exception {
		List<Object[]> list = wrapContactDataForProvider(generateRandomContacts(5));
		return list.iterator();				
	}	



	public static List<Object[]> wrapContactDataForProvider(List<ContactData> contacts) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (ContactData contact : contacts ) {
			list.add(new Object[] {contact});
		}
		return list;
	}
	
	@DataProvider
	public Iterator<Object[]> invalidContactGenerator() {
		List<Object[]> list = new ArrayList<Object[]>();
		list.add(new Object[] {new ContactData()
	    .withFirstName("First name_ '")});
		return list.iterator();				
	}	
	
	
}
